home *** CD-ROM | disk | FTP | other *** search
/ CD Exchange / CD Exchange - Volume 1.iso / games / pd / chess / src.lha / src / init.c < prev    next >
C/C++ Source or Header  |  1992-09-06  |  12KB  |  486 lines

  1. /*
  2.  * init.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include "gnuchess.h"
  24.  
  25. /* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
  26.  
  27.  
  28. short distdata[64][64], taxidata[64][64];
  29.  
  30. #ifdef KILLT
  31. /* put moves to the center first */
  32. void
  33. Initialize_killt (void)
  34. {
  35.   register unsigned short f, t, s;
  36.   register short d;
  37.   for (f = 64; f--;)
  38.     for (t = 64; t--;)
  39.       {
  40.     d = taxidata[f][0x1b];
  41.     if (taxidata[f][0x1c] < d)
  42.       d = taxidata[f][0x1c];
  43.     if (taxidata[f][0x23] < d)
  44.       d = taxidata[f][0x23];
  45.     if (taxidata[f][0x24] < d)
  46.       d = taxidata[f][0x24];
  47.     s = d;
  48.     d = taxidata[t][0x1b];
  49.     if (taxidata[t][0x1c] < d)
  50.       d = taxidata[t][0x1c];
  51.     if (taxidata[t][0x23] < d)
  52.       d = taxidata[t][0x23];
  53.     if (taxidata[t][0x24] < d)
  54.       d = taxidata[t][0x24];
  55.     s -= d;
  56.     killt[(f << 8) | t] = s;
  57.     killt[(f << 8) | t | 0x80] = s;
  58.       }
  59. }
  60. #endif
  61. void
  62. Initialize_dist (void)
  63. {
  64.   register short a, b, d, di;
  65.  
  66.   for (a = 0; a < 64; a++)
  67.     for (b = 0; b < 64; b++)
  68.       {
  69.     d = abs (column (a) - column (b));
  70.     di = abs (row (a) - row (b));
  71.     taxidata[a][b] = d + di;
  72.     distdata[a][b] = (d > di ? d : di);
  73.       }
  74. #ifdef KILLT
  75.   Initialize_killt ();
  76. #endif
  77. }
  78.  
  79. const short Stboard[64] =
  80. {rook, knight, bishop, queen, king, bishop, knight, rook,
  81.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  82.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  85.  rook, knight, bishop, queen, king, bishop, knight, rook};
  86. const short Stcolor[64] =
  87. {white, white, white, white, white, white, white, white,
  88.  white, white, white, white, white, white, white, white,
  89.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  90.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  91.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  92.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  93.  black, black, black, black, black, black, black, black,
  94.  black, black, black, black, black, black, black, black};
  95. short board[64], color[64];
  96.  
  97. /* given epsquare, from where can a pawn be taken? */
  98. const short epmove1[64] =
  99. {0, 1, 2, 3, 4, 5, 6, 7,
  100.  8, 9, 10, 11, 12, 13, 14, 15,
  101.  16, 24, 25, 26, 27, 28, 29, 30,
  102.  24, 25, 26, 27, 28, 29, 30, 31,
  103.  32, 33, 34, 35, 36, 37, 38, 39,
  104.  40, 32, 33, 34, 35, 36, 37, 38,
  105.  48, 49, 50, 51, 52, 53, 54, 55,
  106.  56, 57, 58, 59, 60, 61, 62, 63};
  107. const short epmove2[64] =
  108. {0, 1, 2, 3, 4, 5, 6, 7,
  109.  8, 9, 10, 11, 12, 13, 14, 15,
  110.  25, 26, 27, 28, 29, 30, 31, 23,
  111.  24, 25, 26, 27, 28, 29, 30, 31,
  112.  32, 33, 34, 35, 36, 37, 38, 39,
  113.  33, 34, 35, 36, 37, 38, 39, 47,
  114.  48, 49, 50, 51, 52, 53, 54, 55,
  115.  56, 57, 58, 59, 60, 61, 62, 63};
  116.  
  117.  
  118. /*
  119.  * nextpos[piece][from-square] , nextdir[piece][from-square] gives vector of
  120.  * positions reachable from from-square in ppos with piece such that the
  121.  * sequence    ppos = nextpos[piece][from-square]; pdir =
  122.  * nextdir[piece][from-square]; u = ppos[sq]; do { u = ppos[u]; if(color[u]
  123.  * != neutral) u = pdir[u]; } while (sq != u); will generate the sequence of
  124.  * all squares reachable from sq.
  125.  *
  126.  * If the path is blocked u = pdir[sq] will generate the continuation of the
  127.  * sequence in other directions.
  128.  */
  129.  
  130. unsigned char __far nextpos[8][64][64];
  131. unsigned char __far nextdir[8][64][64];
  132.  
  133. /*
  134.  * ptype is used to separate white and black pawns, like this; ptyp =
  135.  * ptype[side][piece] piece can be used directly in nextpos/nextdir when
  136.  * generating moves for pieces that are not black pawns.
  137.  */
  138. const short ptype[2][8] =
  139. {
  140.   no_piece, pawn, knight, bishop, rook, queen, king, no_piece,
  141.   no_piece, bpawn, knight, bishop, rook, queen, king, no_piece};
  142.  
  143. /* data used to generate nextpos/nextdir */
  144. static const short direc[8][8] =
  145. {
  146.   0, 0, 0, 0, 0, 0, 0, 0,
  147.   10, 9, 11, 0, 0, 0, 0, 0,
  148.   8, -8, 12, -12, 19, -19, 21, -21,
  149.   9, 11, -9, -11, 0, 0, 0, 0,
  150.   1, 10, -1, -10, 0, 0, 0, 0,
  151.   1, 10, -1, -10, 9, 11, -9, -11,
  152.   1, 10, -1, -10, 9, 11, -9, -11,
  153.   -10, -9, -11, 0, 0, 0, 0, 0};
  154. static const short max_steps[8] =
  155. {0, 2, 1, 7, 7, 7, 1, 2};
  156. static const short nunmap[120] =
  157. {
  158.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  159.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  160.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  161.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  162.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  163.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  164.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  165.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  166.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  167.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  168.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  169.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  170.  
  171. int InitFlag = false;
  172. void
  173. Initialize_moves (void)
  174.  
  175. /*
  176.  * This procedure pre-calculates all moves for every piece from every square.
  177.  * This data is stored in nextpos/nextdir and used later in the move
  178.  * generation routines.
  179.  */
  180.  
  181. {
  182.   short ptyp, po, p0, d, di, s, delta;
  183.   unsigned char *ppos, *pdir;
  184.   short dest[8][8];
  185.   short steps[8];
  186.   short sorted[8];
  187.  
  188.   for (ptyp = 0; ptyp < 8; ptyp++)
  189.     for (po = 0; po < 64; po++)
  190.       for (p0 = 0; p0 < 64; p0++)
  191.     {
  192.       nextpos[ptyp][po][p0] = (unsigned char) po;
  193.       nextdir[ptyp][po][p0] = (unsigned char) po;
  194.     }
  195.   for (ptyp = 1; ptyp < 8; ptyp++)
  196.     for (po = 21; po < 99; po++)
  197.       if (nunmap[po] >= 0)
  198.     {
  199.       ppos = nextpos[ptyp][nunmap[po]];
  200.       pdir = nextdir[ptyp][nunmap[po]];
  201.       /* dest is a function of direction and steps */
  202.       for (d = 0; d < 8; d++)
  203.         {
  204.           dest[d][0] = nunmap[po];
  205.           delta = direc[ptyp][d];
  206.           if (delta != 0)
  207.         {
  208.           p0 = po;
  209.           for (s = 0; s < max_steps[ptyp]; s++)
  210.             {
  211.               p0 = p0 + delta;
  212.  
  213.               /*
  214.                * break if (off board) or (pawns only move two
  215.                * steps from home square)
  216.                */
  217.               if ((nunmap[p0] < 0) || (((ptyp == pawn) || (ptyp == bpawn))
  218.                            && ((s > 0) && ((d > 0) || (Stboard[nunmap[po]] != pawn)))))
  219.             break;
  220.               else
  221.             dest[d][s] = nunmap[p0];
  222.             }
  223.         }
  224.           else
  225.         s = 0;
  226.  
  227.           /*
  228.            * sort dest in number of steps order currently no sort
  229.            * is done due to compability with the move generation
  230.            * order in old gnu chess
  231.            */
  232.           steps[d] = s;
  233.           for (di = d; s > 0 && di > 0; di--)
  234.         if (steps[sorted[di - 1]] == 0)    /* should be: < s */
  235.           sorted[di] = sorted[di - 1];
  236.         else
  237.           break;
  238.           sorted[di] = d;
  239.         }
  240.  
  241.       /*
  242.        * update nextpos/nextdir, pawns have two threads (capture
  243.        * and no capture)
  244.        */
  245.       p0 = nunmap[po];
  246.       if (ptyp == pawn || ptyp == bpawn)
  247.         {
  248.           for (s = 0; s < steps[0]; s++)
  249.         {
  250.           ppos[p0] = (unsigned char) dest[0][s];
  251.           p0 = dest[0][s];
  252.         }
  253.           p0 = nunmap[po];
  254.           for (d = 1; d < 3; d++)
  255.         {
  256.           pdir[p0] = (unsigned char) dest[d][0];
  257.           p0 = dest[d][0];
  258.         }
  259.         }
  260.       else
  261.         {
  262.           pdir[p0] = (unsigned char) dest[sorted[0]][0];
  263.           for (d = 0; d < 8; d++)
  264.         for (s = 0; s < steps[sorted[d]]; s++)
  265.           {
  266.             ppos[p0] = (unsigned char) dest[sorted[d]][s];
  267.             p0 = dest[sorted[d]][s];
  268.             if (d < 7)
  269.               pdir[p0] = (unsigned char) dest[sorted[d + 1]][0];
  270.  
  271.             /*
  272.              * else is already initialized
  273.              */
  274.           }
  275.         }
  276.     }
  277. }
  278.  
  279. void
  280. NewGame (void)
  281.  
  282. /*
  283.  * Reset the board and other variables to start a new game.
  284.  */
  285.  
  286. {
  287.   short l, c, p;
  288.  
  289.   stage = stage2 = -1;        /* the game is not yet started */
  290.   flag.illegal = flag.mate = flag.quit = flag.bothsides = false;
  291.   flag.onemove = flag.force = false;
  292.   flag.hash = flag.easy = flag.beep = flag.rc